package com.zhugang.week13.queue;

/**
 * @program algorithms
 * @description: QueueByList
 * @author: chanzhugang
 * @create: 2022/11/01 11:33
 */
public class QueueByList {

    private Node head;
    private Node tail;

    /**
     * 入队
     *
     * @param value
     */
    public void enqueue(int value) {
        Node newNode = new Node(value, null);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            // 尾插
            tail.next = newNode;
            tail = newNode;
        }
    }

    public int dequeue() {
        // 取头节点
        if (head == null) {
            return -1;
        }
        int data = head.data;
        head = head.next;
        if (head == null) {
            // 只有一个节点，特殊处理
            tail = null;
        }
        return data;
    }

    class Node {
        public int data;
        public Node next;

        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}